package orxatas.travelme.activity;

import com.markupartist.android.widget.ActionBar;

import orxatas.travelme.R;
import orxatas.travelme.sync.AsyncNoticeCode;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;

public class ExampleActionbarLib extends Activity implements AsyncActivity {
	
	private boolean progressBarVisibility = false;
	
	ActionBar actionBar;
	
	/**
	 * The loading - dialog.
	 * */
	ProgressDialog dialog = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.exampleactionbarlibrary); //Cargamos nuestro layout.
		
		//obtenemos nuestro action bar
		actionBar = (ActionBar) findViewById(R.id.actionbar);
		actionBar.setTitle("We love Ubuntu", getAssets(), "Ubuntu-M.ttf" );
		//actionBar.setHomeLogo(R.drawable.travelme_logo);
		//actionBar.setTitle("Ejemplo de actividad");

		//Creamos una "acción"
		actionBar.addAction(new ActionBar.Action()/* == import com.markupartist.android.widget.ActionBar.Action */ {
			
			/*
			 * Acción que se realizará cuando se haga "click" en el icono de la acción.
			 * */
			@Override
			public void performAction(View view) {
				if(!progressBarVisibility)
					actionBar.setProgressBarVisibility(View.VISIBLE);
				else actionBar.setProgressBarVisibility(View.GONE);
				
				progressBarVisibility = !progressBarVisibility;
			}
			
			/*
			 * Debe devolver el identificador del icono que se mostrará.
			 * R.drawable.nuestroicono
			 * */
			@Override
			public int getDrawable() {
				return R.drawable.ic_menu_refresh;
			}
		});
	}

	/**
	 * Example of HOWTO manage this.
	 * This will do something if your activity require it. If not, do nothing.
	 * */
	@Override
	public void asyncNotice(AsyncNoticeCode code) {
		switch(code){
			case GROUP_MANAGEMENT_ASYNC:
				//TODO
				break;
			case USERS_CHANGED:
				//TODO
				//Oh! the friend list was changed. I will obtain the new list an redraw part, or all, of views of my activity. IF NEED.
				//Then i will donate money to the poors.
				break;
			case GROUPS_CHANGED:
				//TODO
				break;
		}
	}

	@Override
	public Activity getActivity() {
		return this;
	}

	@Override
	public void syncStarted() {
		actionBar.setProgressBarVisibility(View.VISIBLE);
	}

	@Override
	public void syncEnded() {
		actionBar.setProgressBarVisibility(View.GONE);
	}

	@Override
	public void syncAndWaitCallStarted() {
		if(dialog == null) {
			dialog = ProgressDialog.show(this, "", "Loading. Please wait...", true);
			dialog.setCancelable(false);
		}
	}
	
	final private int SYNCACTION_ACTION1 = 1;
	final private int SYNCACTION_ACTION2 = 2;
	final private int SYNCACTION_ACTION3 = 3;
	/**
	 * When a sync call is ended, with data, this function will be executed.
	 * 
	 * @param o The object i need to receive (or null if I don't need)
	 * @param syncAction The action passed to the synchronous method that identify this action.
	 * */
	@Override
	public void syncAndWaitCallEnded(Object o, int syncAction) {
		if(dialog != null) {
			dialog.cancel();
			dialog = null;
		}
		switch(syncAction){
			case SYNCACTION_ACTION1:
				/*
				
				if(o != null) {
					//Try to obtain the data I want and need.
					try {
						TheType myValue = (TheType) o; //Cast Object to my data type.
						
						myManagerOfMyAction(myValue); //Manager the data i get.
					} catch (ClassCastException e) { //Prevent bad casting
						//Bad Cast
					}
				}
				
				*/
				break;
			case SYNCACTION_ACTION2:
				/*Action 2 code cast*/
				break;
			case SYNCACTION_ACTION3:
				break;
		}
	}
}
